package com.nenu.bdms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nenu.bdms.config.webresult.WebResult;
import com.nenu.bdms.dao.*;
import com.nenu.bdms.module.allocation.Allocation;
import com.nenu.bdms.module.fund.FundVO;
import com.nenu.bdms.module.subexperiment.SubExperiment;
import com.nenu.bdms.module.subexperiment.SubExperimentVO;
import com.nenu.bdms.module.t_e_relation.TesteeExperimentRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Author     ：GluttonK
 * @ Date       ：Created in 16:03 2022/11/24
 */
@Service
public class RecordService {

    @Autowired
    private TestMapper testMapper;
    @Autowired
    private FundMapper fundMapper;
    @Autowired
    private TesteeMapper testeeMapper;
    @Autowired
    private AllocationMapper allocationMapper;
    @Autowired
    private ExperimentMapper experimentMapper;
    @Autowired
    private SubExperimentMapper subExperimentMapper;
    @Autowired
    private TesteeExperimentRelationMapper testeeExperimentRelationMapper;
    @Autowired
    private FundExperimentRelationMapper fundExperimentRelation;

    public WebResult updateStatus(Integer experimentId,Integer testeeId) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("testee_id",testeeId);
        queryWrapper.eq("experiment_id",experimentId);
        TesteeExperimentRelation testeeExperimentRelation = testeeExperimentRelationMapper.selectOne(queryWrapper);
        testeeExperimentRelation.setStatus(true);
        testeeExperimentRelationMapper.updateById(testeeExperimentRelation);
        return WebResult.okWebResult();

    }

    public WebResult saveAllData (Integer experimentId,List<SubExperimentVO>  subExperimentVOList){
        //将三个子实验插入tb_sub_experiment
        int i = 1;
        int decisionMakingCount = experimentMapper.selectById(experimentId).getExperimentDecisionMakingCount();
        for (SubExperimentVO subExperimentVO : subExperimentVOList) {
            SubExperiment subExperiment = new SubExperiment();
            subExperiment.setExperimentId(experimentId);
            subExperiment.setIndex(i);
            subExperiment.setCalculatedAsset(subExperimentVO.getCalculatedAsset());
            subExperiment.setAccumulatedProfit(subExperimentVO.getAccumulatedProfit());
            subExperimentMapper.insert(subExperiment);
            i=i+1;
            Integer subExperimentId = subExperiment.getId();
            ArrayList<FundVO> funds = subExperimentVO.getFunds();
            for (FundVO fundVO : funds
                 ) {
                for (int j = 0; j < decisionMakingCount; j++) {
                    Allocation allocation = new Allocation();
                    allocation.setSubExperimentId(subExperimentId);
                    allocation.setFundId(fundVO.getId());
                    allocation.setPrice(fundVO.getPrice().get(j));
                    allocation.setAllocation(fundVO.getAllocationRate().get(j+1));
                    allocation.setIndex(j+1);
                    allocationMapper.insert(allocation);
                }
            }

        }
        return WebResult.okWebResult();
    }



}
